package com.mo.lib.utils.behavior

import android.R.attr
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.recyclerview.widget.RecyclerView

/**
 * @ author：mo
 * @ data：2022/8/2:10:28
 * @ 功能：
 */
class SampleTitleBehavior(context: Context?, attrs: AttributeSet?) : CoordinatorLayout.Behavior<View>(context, attrs) {
    // 列表顶部和title底部重合时，列表的滑动距离。
    private var deltaY = 0f


    override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View): Boolean {
        return dependency is RecyclerView
    }

    override fun onDependentViewChanged(parent: CoordinatorLayout, child: View, dependency: View): Boolean {
        if (deltaY == 0f) {
            deltaY = dependency.getY() - child.getHeight()
        }
        var dy: Float = dependency.getY() - child.getHeight()
        dy = if (dy < 0)  0F else dy
//        val translationY: Float = -(dy / deltaY) * child.getHeight()
//        child.setTranslationY(translationY)
        val alpha = 1 - dy / deltaY
        child.alpha = alpha

        return true
    }
}